#include "themeselector.h"
#include "thememanager.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QMessageBox>

ThemeSelector::ThemeSelector(QWidget *parent) : QWidget(parent), m_dragPosition(30,30)
{
    // 设置窗口标志，允许拖动
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);

    // 如果设置了Qt::WA_DeleteOnClose属性，窗口会被自动删除，点击应用时close();会触发关闭事件进而释放资源。注释的话就让基类管理
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 设置窗口样式表，使用对象名称限定
    this->setObjectName("ThemeSelector");
    this->setStyleSheet(R"(
                        QWidget#ThemeSelector {
                        background-color: #e8f5e9;   /* 淡绿色背景 */
                        border: 2px solid #4caf50;   /* 绿色边框 */
                        border-radius: 10px;
                        }
                        )");

    // 创建主布局
    QVBoxLayout* layout = new QVBoxLayout(this);

    // 设置布局的边距和间距
    layout->setContentsMargins(5, 5, 5, 5); // 左、上、右、下边距
    layout->setSpacing(5);  // 控件之间的间距

    // 创建标题栏用于拖动
    QWidget* titleBar = new QWidget(this);
    titleBar->setObjectName("TitleBar");
    titleBar->setStyleSheet(R"(
                            QWidget#TitleBar {
                            background-color: transparent;
                            min-height: 20px;
                            }
                            )");
    titleBar->setCursor(Qt::SizeAllCursor);

    // 创建标题栏布局
    QHBoxLayout* titleLayout = new QHBoxLayout(titleBar);
    titleLayout->setContentsMargins(0, 0, 0, 0);
    titleLayout->setSpacing(0);

    // 添加一个标签显示窗口标题
    QLabel* titleLabel = new QLabel(tr("主题选择"), titleBar);
    titleLabel->setObjectName("TitleLabel");
    titleLabel->setStyleSheet(R"(
                              QLabel#TitleLabel {
                              color: #2e7d32;
                              font-weight: bold;
                              padding-left: 5px;
                              }
                              )");
    titleLayout->addWidget(titleLabel);
    titleLayout->addStretch();

    // 将标题栏添加到主布局
    layout->addWidget(titleBar);

    // 创建主题列表控件
    m_themeList = new QListWidget(this);
    // 设置滚动条策略：只在需要时显示
    m_themeList->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    m_themeList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_themeList->setStyleSheet(R"(
                               QListWidget {
                               background-color: white;
                               border: 1px solid #81c784;
                               border-radius: 3px;
                               padding: 5px;
                               color: #2e7d32;
                               font-size: 12px;
                               outline: none;
                               }
                               QListWidget::item {
                               background-color: white;
                               padding: 5px;
                               border-bottom: 1px solid #e0e0e0;
                               color: #2e7d32;
                               outline: none;
                               border-radius: 3px;
                               }
                               QListWidget::item:selected {
                               background-color: green;
                               color: white;
                               outline: none;
                               }
                               QListWidget::item:selected:hover {
                               background-color: #a5d6a7;
                               border: 1px solid #66bb6a;
                               }
                               QListWidget::item:hover {
                               background-color: #f1f8e9;
                               outline: none;
                               }
                               /* 滚动条样式 */
                               QListWidget::verticalScrollBar:vertical {
                               border: none;
                               background: green;  /* 使用与选中项相同的绿色背景 */
                               width: 8px;
                               margin: 0px;
                               }
                               QListWidget::verticalScrollBar::handle:vertical {
                               background: rgba(255, 255, 255, 0.7);  /* 半透明白色滑块 */
                               min-height: 20px;
                               border-radius: 4px;
                               }
                               QListWidget::verticalScrollBar::handle:vertical:hover {
                               background: rgba(255, 255, 255, 0.9);  /* 悬停时更不透明 */
                               }
                               QListWidget::verticalScrollBar::add-line:vertical,
                               QListWidget::verticalScrollBar::sub-line:vertical {
                               height: 0px;
                               background: none;
                               }
                               QListWidget::verticalScrollBar::add-page:vertical,
                               QListWidget::verticalScrollBar::sub-page:vertical {
                               background: none;
                               }
                               )");

    // 列出所有可用主题
    this->populateThemeList();
    layout->addWidget(m_themeList);

    // 创建并连接应用按钮
    QPushButton* applyButton = new QPushButton(tr("应用"), this);
    applyButton->setStyleSheet(R"(
                               QPushButton {
                               background-color: #4caf50;  /* 绿色背景 */
                               color: white;               /* 白色文字 */
                               border: none;               /* 无边框 */
                               border-radius: 3px;         /* 圆角 */
                               padding: 5px 15px;          /* 内边距 */
                               font-weight: bold;
                               outline: none;
                               }
                               QPushButton:focus {
                               outline: none;  /* 确保获取焦点时也没有轮廓 */
                               }
                               QPushButton:hover {
                               background-color: #45a049;  /* 鼠标悬停时的深绿色 */
                               }
                               QPushButton:pressed {
                               background-color: #388e3c;  /* 点击时的更深绿色 */
                               }
                               )");
    connect(applyButton, &QPushButton::clicked, this, [this]() {
        // 检查是否有选中的主题
        if (!m_themeList->currentItem()) {
            QMessageBox::warning(this, "警告", "请先选择一个主题！");
            return;
        }

        // 获取选中的主题
        QString selectedTheme = m_themeList->currentItem()->text();

        // 显示确认对话框
        QMessageBox::StandardButton reply;
        reply = QMessageBox::question(this,
                                    "确认应用主题",
                                    QString("%1").arg(selectedTheme),
                                    QMessageBox::Yes|QMessageBox::No,
                                    QMessageBox::No);

        if (reply == QMessageBox::Yes) {
            // 用户确认，应用主题
            emit themeSelected(selectedTheme);
            // 关闭对话框
            this->close();
        }
    });

    // 将按钮布局添加到主布局
    layout->addWidget(applyButton);

    // 设置布局
    this->setLayout(layout);

    // 根据需要调整窗口
    this->setFixedSize(QSize(200, 250));
    this->move(m_dragPosition);
}

/**
 * @brief 填充主题列表
 */
void ThemeSelector::populateThemeList() {
    // 获取所有可用主题
    QStringList themes = ThemeManager::instance().availableThemes();
    // 遍历主题列表并添加到控件中
    for (const QString& theme : themes) {
        QListWidgetItem* item = new QListWidgetItem(theme, m_themeList);
        m_themeList->addItem(item);
    }
}

// 添加鼠标事件处理函数以实现窗口拖动
void ThemeSelector::mousePressEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_dragPosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
}

void ThemeSelector::mouseMoveEvent(QMouseEvent* event)
{
    if (event->buttons() & Qt::LeftButton)
    {
        move(event->globalPos() - m_dragPosition);
        event->accept();
    }
}

void ThemeSelector::mouseReleaseEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_dragPosition = QPoint();
        event->accept();
    }
}
